dofile (getScriptPath() .. "\\..\\lib\\lib.lua")

dofile (getScriptPath() .. "\\..\\processor\\processor.lua")
dofile (getScriptPath() .. "\\..\\processor\\watchdog.lua")
dofile (getScriptPath() .. "\\..\\processor\\log.lua")
dofile (getScriptPath() .. "\\..\\processor\\notify.lua")
dofile (getScriptPath() .. "\\..\\processor\\OnTrade.lua")
dofile (getScriptPath() .. "\\..\\processor\\OnConnected.lua")
dofile (getScriptPath() .. "\\..\\processor\\OnDisconnected.lua")

script_name = string.gsub(string.gsub(debug.getinfo(1).short_src, "%.[^%.]*$", ""), "^.*[\\/]", "")

notify = {label_text = {"^nf"}}--, "^tev"}}

processor:AddInit(function()
	if not client_code then
		client_code = lib:GetUser()
		if not client_code then
			return true
		end
		script_id = client_code .. "#" .. script_name
		log_file = string.gsub(debug.getinfo(1).short_src, "[^\\/]*$", "\\..\\data\\log\\" .. script_id .. ".log")
	end
end)

processor:AddInit(function()
	notify.charts = lib:GetChartList("CHART", "VIEW", "NOTIFY")
	if #notify.charts < 1 then
		return true
	end
	--message(lib:toline(notify.charts))
end)

processor:AddInit(function()
	for i, v in ipairs(notify.charts) do
		notify.charts[v] = {index = i} --, delay = 0}
	end
end)

processor:AddInit(function()
	processor:InitLog(log_file)
	processor:InitWatchdog(script_id)
end)

processor:AddInit(function()
	processor:AddHandler("OnConnected", function(flag)
		processor:notify("connected " .. tostring(flag))
	end)

	processor:AddHandler("OnDisconnected", function()
		processor:notify("disconnected")
		lib:AddLine(lib.path.events .. "disconnected", client_code)
	end)

	processor:AddHandler("OnTrade", function(trade)
		local side = -1
		if trade.side_qualifier == 1 then side = 1 end
		processor:notify(trade.sec_code .. " " .. trade.price .. " " .. int(side * trade.qty) .. " " .. trade.value)
		notify.pl = 3
	end)
end)

processor:AddInit(function()
	processor:notify(script_name .. " started")
end)

processor:AddTimer(function()
	for i, v in ipairs(notify.charts) do
		if notify.charts[v].delay and processor:IsDelay(notify.charts[v].delay, 10 * 60 * 1000) then
			notify.charts[v].delay = nil
		end
		
		local c1 = lib:GetLastCandle(v)
		local c2 = lib:GetLastCandle(v, 1)
		if c1 and c2 then
			local high = math.max(c1.high, c2.high)
			local low = math.max(c1.low, c2.low)
			
			if not notify.charts[v].delay then
				if not notify.charts[v].high or not notify.charts[v].low then
					notify:CheckNotifyChart(v, low, high)
				elseif notify.charts[v].high < high then
					notify:CheckNotifyChart(v, nil, high)
				elseif notify.charts[v].low > low then
					notify:CheckNotifyChart(v, low, nil)
				end
			end
			
			notify.charts[v].high = high
			notify.charts[v].low = low
		end
	end
end)
--[[
processor:AddTimer(function()
	if notify.g_count then
		notify.g_count = notify.g_count - 1
		if notify.g_count <= 0 then
			notify.g_count = nil
		end
	else
		collectgarbage("collect")
		notify.g_count = 10
	end
end)
--]]
processor:AddTimer(function()
	if notify.pl then
		notify.pl = notify.pl - 1
		if notify.pl < 0 then
			notify.pl = nil
			local pl, rc = lib:GetPL()
			processor:notify(message(string.format("P/L %s %.2f%% %.0f", client_code, rc, pl)))
		end
	end
end)

function notify:CheckNotifyChart(tag, low, high)
	if not tag or not (low or high) then return end
	local lp
	local instrument = lib:GetChartInstrument(tag)
	for label_id = lib:GetMaxLabelID(tag) * 2, 0, -1 do
		lp = GetLabelParams(tag, label_id)
		if lp and lp.yvalue then
			lp.yvalue = tonumber(lp.yvalue)
			lib:SetMaxLabelID(tag, label_id)
			for _, t in ipairs(notify.label_text) do
				if string.match(lp["text"], t) then
					--message(lp["text"] .. " " .. type(lp.yvalue) .. " " .. lp.yvalue .. " low " .. tostring(low) .. " high " .. tostring(high))
					if low and high then
						if lp.yvalue >= low and lp.yvalue <= high then
							self:Notify(tag, lp.yvalue)
						end
					elseif high then
						if lp.yvalue > notify.charts[tag].high and lp.yvalue <= high then
							self:Notify(tag, lp.yvalue)
						end
					else
						if lp.yvalue >= low and lp.yvalue < notify.charts[tag].low then
							self:Notify(tag, lp.yvalue)
						end
					end
				end
			end
		end
	end
end

function notify:Notify(tag, price)
	local instrument = lib:GetChartInstrument(tag)
	notify.charts[tag].delay = processor:GetTime()
	processor:notify(instrument.code .. " " .. lib:FormatPrice(price, instrument) .. " " .. instrument.short_name)
end


